import csv
import re
import time

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from pom.base import BaseDriver
from pom.User.UserCompetationPage import UserCompetationPage

class UserCompetation(BaseDriver):

    def __init__(self, driver):
        super(UserCompetation, self).__init__(driver)
        self.uc = UserCompetationPage(driver)

    # 参赛须知


    # 进入竞赛查看竞赛基本信息
    # def competation_abse_info(self):
    #     info = {}
    #     self.uc.title

    # 点击某一题
    def click_question(self, num):
        self.Click(self.uc.question(num))
    
    # 单选
    def answer_radio(self, option):
        self.logger.info("单选题")
        self.Click(self.uc.option(option))
        self.logger.info("选择了{}".format(option))
        
    # 多选
    def answer_checkbox(self, option):
        self.logger.info("多选题")
        lens = len(option)
        for i in range(lens):
            self.Click(self.uc.option(option[i]))
            self.logger.info("选择了{}".format(option[i]))

    # 答填空题
    def answer_textarea(self, text):
        self.logger.info("填空题")
        self.Sendkeys(self.uc.textarea, text)

    # 答主观题
    def answer_subjective_textarea(self, text):
        self.logger.info("主观题")
        self.Sendkeys(self.uc.subjective_textarea, text)

    # 提交答案
    def submit_answer(self):
        self.Click(self.uc.submit_answer)
        # WebDriverWait(self.driver, 2).until(self.uc._frequent_operation)

    # 获取题号
    def question_num(self):
        num_text = self.uc.question_num.text
        tmp = num_text.find('.')
        num = num_text[0:tmp]
        return num
    # 点击下一题
    def next_question(self):
        self.Click(self.uc.next_question)
        self.logger.info("点击下一题")

    # 点击上一题
    def front_question(self):
        self.Click(self.uc.front_question)
        self.logger.info("点击上一题")

    # 有未完成的题目结束答题之前的确认弹框, 返回提示框中未答题的数量

    def unfinished_num(self):
        text = self.uc.unfinished_num.text
        num = re.sub('\D', '', text)
        print(num)
        return num

    def unfinished_stop_confirm(self):
        self.Click(self.uc.unfinished_confirm_button)


    # 结束答题
    def stop_answer_button(self):
        WebDriverWait(self.driver, 2).until(EC.presence_of_element_located(self.uc._stop_button))
        self.Click(self.uc.stop_button)

    def confirm_compatation_name(self):
        WebDriverWait(self.driver, 2).until(EC.visibility_of_element_located(self.uc._stop_name_text))
        self.Sendkeys(self.uc.stop_name_text, self.uc.stop_confirm_name.text)
        self.Click(self.uc.stop_confirm_button)
        self.logger.info("结束答题")


    # 答题
    def answer(self, file, question_num):

        # 点击第一题进行答题操作
        self.Click(self.driver.find_element_by_xpath('//div[@class="ques-detail f-csp"]'))
        # 从答案文件中获取该作答的答案
        with open(file) as r:
            csv_reader = csv.reader(r)
            num = 0
            blank_num = 0
            for i in csv_reader:
                num+=1
                time.sleep(1)
                tmp = int(self.question_num())
                if num != tmp:
                    self.next_question()
                i[1] = eval(i[1])
                if i[1] == 'None' and num == question_num:
                    blank_num += 1
                    self.driver.refresh()
                    break
                elif i[1] == 'None':
                    blank_num += 1
                    self.next_question()
                    continue
                # WebDriverWait(self.driver, 2).until(EC.text_to_be_present_in_element(self.uc._question_num, str(num)))
                type = self.uc.question_type.text

                if "单选" in type:
                    self.answer_radio(i[1])
                    self.submit_answer()
                    continue
                elif "多选" in type:
                    for j in range(len(i[1])):
                        self.answer_radio(i[1][j])
                    self.submit_answer()
                    continue
                elif "填空" in type:
                    self.answer_textarea(i[1])
                    self.submit_answer()
                    continue
                elif "主观" in type:
                    self.answer_subjective_textarea(i[1])
                    self.submit_answer()
                    continue
                else:
                    raise Exception
            # self.driver.refresh()
            self.screenshot()
            self.stop_answer_button()
            if blank_num != 0:
                self.unfinished_stop_confirm()
            self.confirm_compatation_name()